#coding=utf-8
import sys
import os
from   os.path import abspath, dirname

import devices_serial

sys.path.append(abspath(dirname(__file__)))
import tkinter
import tkinter.filedialog
from   tkinter import *
import Fun
ElementBGArray={}  
ElementBGArray_Resize={} 
ElementBGArray_IM={} 

import device_http
import GridBase
import DbBase
from   PIL import Image,ImageTk
DbBase.initDb()
G_UITreeImageArray={}
def Form_1_onLoad(uiName):
    treeview = GridBase.clearData(uiName, 'devices_list')
    res = DbBase.getData()
    for item in res:
        treeview.insert('', 'end', values=(item[0],item[1], item[2],item[3],item[4],item[5] ))
        devices_serial.openListSerial(item[3],item[4])
    if len(res)>0:
        treeview.selection_set(treeview.get_children()[0])
        Button_17_onCommand(uiName,'onLoad')
def TreeView_15_onButton3(event,uiName,widgetName):
    #Create the Popup Menu
    widget = Fun.GetElement(uiName,widgetName)
    PopupMenu=tkinter.Menu(widget,tearoff=False)
    PopupMenu.add_command(label="修改",command=lambda:Menu_修改(uiName,"修改"))
    PopupMenu.add_command(label="删除",command=lambda:Menu_删除(uiName,"删除"))
    item=GridBase.getSelected(uiName, 'devices_list')
    if item!=None:
        print(item[3])
        ser=devices_serial.getListSerial(item[3])
        print(ser)
        if ser:
            PopupMenu.add_command(label="停止", command=lambda: Menu_停止(uiName, "停止"))
        else:
            PopupMenu.add_command(label="监听", command=lambda: Menu_监听(uiName, "监听"))
    PopupMenu.post(event.x_root,event.y_root)
def Menu_停止(uiName,widgetName):
    item = GridBase.getSelected(uiName, 'devices_list')
    devices_serial.closeListSerial(item[3])
def Menu_监听(uiName, widgetName):
    item = GridBase.getSelected(uiName, 'devices_list')
    devices_serial.openListSerial(item[3],item[4])

def TreeView_15_onDoubleButton1(event,uiName,widgetName):
    Menu_修改(uiName,'双击')


def Button_17_onCommand(uiName,widgetName):
    Button_17 = Fun.GetElement(uiName, 'Button_17')
    http_port = Fun.GetText(uiName, 'http_port')
    #启动服务器
    if device_http.serialServer==None:
        if http_port == '\n':
            Fun.MessageBox('请输入服务端口号')

        device_http.startHttpServer(http_port)
        ElementBGArray[17] = Image.open(Fun.resource_path("stop.png")).convert('RGBA')
        ElementBGArray_Resize[17] = ElementBGArray[17].resize((48, 48), Image.ANTIALIAS)
        ElementBGArray_IM[17] = ImageTk.PhotoImage(ElementBGArray_Resize[17])
        Button_17.configure(image=ElementBGArray_IM[17])
    else:
        device_http.stopHttpServer()
        ElementBGArray[17] = Image.open(Fun.resource_path("start.png")).convert('RGBA')
        ElementBGArray_Resize[17] = ElementBGArray[17].resize((48, 48), Image.ANTIALIAS)
        ElementBGArray_IM[17] = ImageTk.PhotoImage(ElementBGArray_Resize[17])
        Button_17.configure(image=ElementBGArray_IM[17])
    # root = Fun.GetElement(uiName, 'root')
    # root.update()

def Menu_新建(uiName,itemName):
    topLevel = tkinter.Toplevel()
    topLevel.attributes("-toolwindow", 1)
    topLevel.wm_attributes("-topmost", 1)
    import add_device
    add_device.add_device(topLevel)
    tkinter.Tk.wait_window(topLevel)
def Menu_修改(uiName,itemName):
    item = GridBase.getSelected(uiName, 'devices_list')
    if (item == None):
        Fun.MessageBox("请先选择数据,在进行修改!")
        return
    topLevel = tkinter.Toplevel()
    topLevel.attributes("-toolwindow", 1)
    topLevel.wm_attributes("-topmost", 1)
    import add_device
    add_device.add_device(topLevel)
    # 设置修改页的值
    Fun.SetText('add_device', 'device_type', item[1])
    Fun.SetText('add_device', 'device_name', item[2])
    Fun.SetText('add_device', 'device_serial_port', item[3])
    Fun.SetText('add_device', 'device_serial_bps', item[4])
    Fun.SetText('add_device', 'device_remark', item[5])
    Fun.SetText('add_device', 'Button_12', '修  改')
    root = Fun.GetElement('add_device', 'root')
    root.title("修改设备")
    tkinter.Tk.wait_window(topLevel)
def Menu_删除(uiName,itemName):
    item = GridBase.getSelected(uiName, 'devices_list')
    DbBase.deleteAccount(item[0])
    devices_serial.closeListSerial(item[3])
    GridBase.deleteSelected(uiName, 'devices_list')
    treeview = Fun.GetElement(uiName, 'devices_list')
    if len(treeview.get_children()) > 0:
        treeview.selection_set(treeview.get_children()[0])
def Menu_关于(uiName,itemName):
    Fun.MessageBox('limis-检测设备连接服务 1.0.0 python3.8.6 32位')
def Menu_文档(uiName,itemName):
    getProfile()
def getProfile():
    os.system(Fun.resource_path("help.docx"))